package cn.itcast.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description:
 * Created by t410 on 2017/7/5.
 */
@Controller
public class I18nController {
    @RequestMapping(value = "/hello")
    public ModelAndView welcome(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("i18n/welcome");
        RequestContext requestContext = new RequestContext(request);
        String message = requestContext.getMessage("welcome");  //获取国际化信息
        System.out.println(message);
        System.out.println("aaaaaaaaaa");
        return modelAndView;
    }

    @RequestMapping("/localeResolverTest")
    public String test(HttpServletRequest request, HttpServletResponse response, @RequestParam(value="lang",required=false) String lang) {
        if(lang != null) {
            LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
            localeResolver.setLocale(request, response, StringUtils.parseLocaleString(lang));
            /*if(lang.equals("zh_CN")) {
                //localeResolver.setLocale(request, response, StringUtils.parseLocaleString(newLocale));
            }
            if(lang.equals("en_US")) {
                Locale locale = new Locale("en", "US");
                //local.setLocale(request, response, locale);
            }*/
        }
        //后台获取国际化的信息
        RequestContext requestContext = new RequestContext(request);
        String message = requestContext.getMessage("welcome");  //获取国际化信息
        System.out.println(message);
        System.out.println("aaaaaaaaaa");

        return "i18n/welcome";
    }

}
